# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Rasmus Thomsen <oss@cogitri.dev>
pkgname=mozjs60
pkgver=60.9.0
_majver=${pkgver%%.*}
pkgrel=7
pkgdesc="standalone mozilla javascript engine"
url="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Releases/60"
arch="all"
license="MPL-2.0"
depends_dev="icu-dev nspr-dev libffi-dev>=3.3 readline-dev"
makedepends="$depends_dev zlib-dev python2 python2-dev perl sed autoconf2.13 linux-headers"
subpackages="$pkgname-dev $pkgname-dbg"
source="https://ftp.mozilla.org/pub/firefox/releases/${pkgver}esr/source/firefox-${pkgver}esr.source.tar.xz
	0001-silence-sandbox-violations.patch
	fix-musl-build.patch
	fix-soname-lib.patch
	big-endian.patch
	"
builddir="$srcdir"/firefox-$pkgver
_builddir="$builddir/js/src"

# secfixes:
#   60.7.2-r0:
#     - CVE-2019-11708
#     - CVE-2019-11707

build() {
	cd "$_builddir"

	export LDFLAGS="$LDFLAGS -Wl,-z,stack-size=1048576"

	# avoid complains about autoconf
	touch configure

	# Fix broken ARM_ARCH check which doesn't work with newer GCCs it seems.
	# Disable JIT on MIPS, doesn't compile.
	extra_args=""
	case "$CTARGET" in
		armv*)
			ARM_ARCH=$(echo ${CTARGET#*armv} | sed "s|-.*||")
			sed -i "s|ARM_ARCH=.*|ARM_ARCH=$ARM_ARCH|" "$builddir"/build/autoconf/arch.m4
			;;
		mips*)
			extra_args="--disable-ion"
			;;
	esac

	export SHELL=/bin/ash
	PYTHON=/usr/bin/python2 ./configure --prefix=/usr \
		--with-system-icu \
		--with-system-nspr \
		--with-system-zlib \
		--with-intl-api \
		--enable-ctypes \
		--enable-shared-js \
		--enable-readline \
		--enable-system-ffi \
		--disable-optimize \
		--disable-jemalloc \
		--enable-pie $extra_args
	make
}

check() {
	cd "$_builddir"
	dist/bin/jsapi-tests
}

package() {
	cd "$_builddir"
	make DESTDIR="$pkgdir" install
	rm -f "$pkgdir"/usr/lib/*.ajs
}

sha512sums="4baea5c9c4eff257834bbaee6d7786f69f7e6bacd24ca13c2705226f4a0d88315ab38c650b2c5e9c76b698f2debc7cea1e5a99cb4dc24e03c48a24df5143a3cf  firefox-60.9.0esr.source.tar.xz
adaacd6e087a07bd4ded598f6a66ee00c67c9092bb93d88729668516f6f00f497ad8ece1866680e6c371e4705e0f9194ade41ea3a986f793bd972c92029cf03a  0001-silence-sandbox-violations.patch
bc91c2fb15eb22acb8acc36d086fb18fbf6f202b4511d138769b5ecaaed4a673349c55f808270c762616fafa42e3b01e74dc0af1dcbeea1289e043926e2750c8  fix-musl-build.patch
4782794a0f409f767293fb5f61a9ad58985e05197538975ed8f7372bfae6921a3b9bcbbbfcf8ce2843cdfe8ee799d08cee71a6391ed5ae939f051d13038b0960  fix-soname-lib.patch
d9d587b614e2a08131688cac9bc3c78ffea96a79c7bcf8ae51ce208de5001250e0cd6f03269e1e09a387afcbc003c6f4446eb25411f65ad3073ed06539d7f74b  big-endian.patch"
